package com.yang.service.Impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yang.constants.SystemConstants;

import com.yang.domain.VO.CategoryVo;
import com.yang.domain.VO.PageVo;
import com.yang.domain.entity.Article;
import com.yang.mapper.CategoryMapper;
import com.yang.service.ArticleService;
import com.yang.service.CategoryService;
import com.yang.domain.entity.Category;
import com.yang.utils.BeanCopyUtils;
import io.jsonwebtoken.lang.Strings;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import com.yang.domain.Result;
import static com.yang.constants.SystemConstants.STATUS_NORMAL;


/**
 * <p>
 * 分类表(Category)表服务实现类
 * </p>
 * @author yang
 * @date 2023/5/29
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Resource
    private ArticleService articleService;

    @Override
    public Result getCategoryList() {
        //1.查询文章表，找到所有已经发布的文章
        LambdaQueryWrapper<Article> articleWrapper = new LambdaQueryWrapper<>();
        articleWrapper.eq(Article::getStatus, SystemConstants.ARTICLE_STATUS_NORMAL);
        List<Article> articleList = articleService.list(articleWrapper);

        //2.从已经发布的文章分类id中找到分类id，去重
        Set<Long> categoryIds = articleList.stream()
                .map(Article::getCategoryId).collect(Collectors.toSet());


        //3.拿到分类id查询分类表
        List<Category> categoryList = listByIds(categoryIds);

        //4.过滤掉所有状态不正常的分类
        List<Category> newCategoryList = categoryList.stream().
                filter(category -> STATUS_NORMAL.equals(category.getStatus())).collect(Collectors.toList());

        //4.category--->categoryVO
        List<CategoryVo> categoryVOList = BeanCopyUtils.copyBeanList(newCategoryList, CategoryVo.class);

        //5.进行返回
        return Result.okResult(categoryVOList);
    }

    @Override
    public List<CategoryVo> listAllCategory() {
        //1.查询文章表，找到所有已经发布的文章
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Category::getStatus, SystemConstants.NORMAL);
        List<Category> list = list(wrapper);

        //2.封装成VO类
        return BeanCopyUtils.copyBeanList(list, CategoryVo.class);
    }


    @Override
    public PageVo selectCategoryPage(Category category, Integer pageNum, Integer pageSize) {

        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();

        wrapper.eq(Strings.hasText(category.getStatus()),Category::getStatus,category.getStatus());
        wrapper.like(Strings.hasText(category.getName()),Category::getName,category.getName());

        Page<Category> page = new Page<>(pageNum,pageSize);
        page(page,wrapper);

        return new PageVo(page.getRecords(),page.getTotal());

    }


}
